
$(function (){
    parseLineData();
});
/**
 * 请求、解析、加载折线图数据
 */
function parseLineData() {
    $.ajax(
        {
            "url":"/dcits/get/line/data",
            "type":"get",
            "success":function (result) {
                var rtnCode = result.rtnCode;
                if(rtnCode == '0'){
                    var dataIn = result.bean.dataIn;
                    var dataOut = result.bean.dataOut;
                    var xAxisdata = result.bean.dateList;
                    var legenddata = ['收入','支出'];
                    var seriesdata = [
                        {
                            name: '收入',
                            type: 'line',
                            stack: '总量',
                            data: dataIn
                        },
                        {
                            name: '支出',
                            type: 'line',
                            stack: '总量',
                            data: dataOut
                        }
                    ];
                    initLine(legenddata, xAxisdata, seriesdata);
                }else {
                    whenLineError();
                }
            },
            "error":function (result) {
                whenLineError();
            }
        }
    );
}
/**
 * 当折线图数据因为某些原因请求失败时初始化一个默认的全0数据加载上去
 */
function whenLineError() {
    var legenddata = ['收入', '支出'];
    var xAxisdata = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
    var seriesdata = [
        {
            name: '收入',
            type: 'line',
            stack: '总量',
            data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
            name: '支出',
            type: 'line',
            stack: '总量',
            data: [220, 182, 191, 234, 290, 330, 310]
        }
    ];
    initLine(legenddata, xAxisdata, seriesdata);
}
/**
 *
 * @param legenddata 折线图内部统计什么维度 例如：收入  住处  字符串型数组
 * @param xAxisdata 折线图横坐标选项数组
 * @param seriesdata  折线图数据，理论上是二维数组 legenddata x xAxisdata
 */
function initLine(legenddata, xAxisdata, seriesdata) {
    var dom = document.getElementById("lineStaticties");
    var myChart = echarts.init(dom);
    option = null;
    option = {
        title: {
            text: '近一周收支统计'
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: legenddata
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xAxisdata
        },
        yAxis: {
            type: 'value'
        },
        series: seriesdata
    };
    if (option && typeof option === "object") {
        myChart.setOption(option, true);
    }
}